<template>
  <div class="login-wrap">
    <img class="login-bg" src="../../assets/img/login-bg3.png" />
    <div class="form-wrap">
      <div class="left">
        <div class="notice-title">
          <div class="text">系统公告</div>
        </div>
      </div>
      <div class="right">
        <div class="login-text">赛尔号Xin服</div>
        <div class="tabs">
          <div
            class="tabs-item"
            :class="loginType === 'login' ? 'active' : ''"
            @click="loginType = 'login'"
          >
            登录
          </div>
          <div
            class="tabs-item"
            :class="loginType === 'reg' ? 'active' : ''"
            @click="loginType = 'reg'"
          >
            注册
          </div>
        </div>
        <el-form :model="form" ref="ruleForm" :rules="rules">
          <el-form-item prop="account">
            <el-input placeholder="请输入账号" v-model.number="form.account">
              <template #suffix>
                <span class="iconfont icon-zhanghao"></span>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              placeholder="请输入密码"
              v-model="form.password"
              type="password"
              show-password
            >
              <template #suffix>
                <span class="iconfont icon-mima1"></span>
              </template>
            </el-input>
          </el-form-item>
          <button @click="submit($event, ruleForm)">
            <span class="now">启动!</span>
            <span class="play">赛尔号</span>
          </button>
        </el-form>
      </div>
    </div>
    <div class="copyright">
      <div class="text">
        <span class="iconfont icon-banquan"></span>版权信息：xxx公司
      </div>
      <div class="record">备案号：11111</div>
    </div>
    <DragButton />
  </div>
</template>

<script setup>
import store from "@/store";
import DragButton from "./components/DragButton.vue";
import { useRouter } from "vue-router";
const $router = useRouter();
const ruleForm = ref();
const loginType = ref("login");
const form = ref({
  account: "",
  password: "",
});
const loginInfo = localStorage.getItem("loginInfo");
if (!!loginInfo) {
  form.value = JSON.parse(loginInfo);
}
const validateAccount = (rule, value, callback) => {
  if (value === "") return callback(new Error("请输入账号"));
  if (isNaN(value)) return callback(new Error("账号必须为数字。"));
  if (value.toString().length !== 9)
    return callback(new Error("账号必须为9位数字。"));
  callback();
};
const rules = {
  account: [{ validator: validateAccount, trigger: "blur" }],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 1, max: 10, message: "密码不能超过10位。", trigger: "blur" },
  ],
};
const stringToHex = (str) => {
  var hexString = "";
  for (var i = 0; i < str.length; i++) {
    // 获取字符的 Unicode 编码并转换为 16 进制
    hexString += str.charCodeAt(i).toString(16);
  }

  // 手动补充零，直到长度为 24
  while (hexString.length < 24) {
    hexString = "0" + hexString; // 在前面添加零
  }

  return hexString;
};

const generateOldSession = (account, password) => {
  // 步骤 1: 账号转换为24字节长度的字节集（自动补零）
  var accountBytes = stringToHex(password);

  // 步骤 3: 密码字节集转为十六进制
  var num = Number(account);
  var hexString = num.toString(16);
  // 填充零，确保长度为 8 位
  while (hexString.length < 8) {
    hexString = "0" + hexString;
  }

  // 步骤 5: 拼接 Oldsession
  var oldSession = hexString + hexString + accountBytes;

  return oldSession;
};

const submit = async (e, el) => {
  e.preventDefault(); // 阻止表单提交
  await el.validate((valid, fields) => {
    if (valid) {
      // 生成Oldsession，算法步骤
      const oldSession = generateOldSession(
        form.value.account,
        form.value.password
      );
      store("user").setSession(oldSession);
      localStorage.setItem("loginInfo", JSON.stringify(form.value));
      $router.push({ name: "home" });
    } else {
      console.log("error submit!", fields);
    }
  });
};
</script>

<style scoped lang="scss">
.login-wrap {
  position: relative;
  width: 100%;
  height: 100vh;
  overflow: hidden;
  .login-bg {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    filter: blur(5px);
  }
  .form-wrap {
    display: flex;
    justify-content: space-between;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 850px;
    height: 60vh;
    border-radius: 8px;
    background-color: #fff;
    box-shadow: 0 2px 12px 10px rgba(0, 0, 0, 0.3);
    .left {
      flex: 1;
      padding: 12px;
      background-color: rgb(240, 242, 245);
      border-right: 1px solid #c9c8c8;
      border-radius: 8px 0 0 16px;
      .notice-title {
        font-size: 18px;
        font-weight: bold;
        color: #303133;
        padding: 8px 12px 16px 0;
        border-bottom: 1px solid #c9c8c8;
      }
    }
    .right {
      padding: 24px 36px;
      width: 400px;
      .login-text {
        font-size: 32px;
        font-weight: bold;
        margin-bottom: 24px;
        text-align: center;
        font-family: AliFangYuan;
        &::after {
          content: "";
          display: inline-block;
          position: relative;
          top: 16px;
          margin-left: 12px;
          width: 58px;
          height: 60px;
          background-image: url(../../assets/img/jst.png);
          background-position: center;
          background-repeat: no-repeat;
          background-size: 100% 100%;
        }
      }
      .tabs {
        display: flex;
        align-items: center;
        justify-content: center;
        margin-bottom: 32px;
        .tabs-item {
          padding: 8px 12px;
          margin: 0 8px;
          cursor: pointer;
          font-size: 18px;
          color: #a0a1a5;
          font-weight: bold;
        }
        .active {
          position: relative;
          color: var(--el-color-primary);
          &::before {
            content: "";
            position: absolute;
            left: 4px;
            bottom: 2px;
            width: 52px;
            height: 2px;
            background-color: var(--el-color-primary);
            border-radius: 2px;
          }
        }
      }
      .el-form-item {
        margin-bottom: 24px;
      }
    }
    :deep(.el-form-item.is-error .el-input__wrapper) {
      box-shadow: none;
    }
    :deep(.el-input) {
      --el-input-border-color: none;
      --el-input-hover-border-color: none;
      --el-input-focus-border-color: none;
      border-bottom: 1px solid #ccc;
      .el-input__wrapper {
        padding: 12px 16px;
        padding-left: 6px;
        background-color: transparent;
      }
      .el-input__inner {
        font-size: 16px;
      }

      input::placeholder {
        color: #333 !important; /* 使用!important提升优先级 */
      }
    }
  }
  .copyright {
    position: absolute;
    bottom: 0;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 12px;
    color: #ccc;
    line-height: 16px;
    text-align: center;
    cursor: default;
    .text {
      position: relative;
      display: flex;
      align-items: center;
      .iconfont {
        margin-right: 6px;
      }
      &::before {
        content: "";
        position: absolute;
        top: 16px;
        left: -100px;
        display: inline-block;
        width: 80px;
        height: 1px;
        background-color: #ccc;
      }
      &::after {
        content: "";
        position: absolute;
        top: 16px;
        right: -100px;
        display: inline-block;
        width: 80px;
        height: 1px;
        background-color: #ccc;
      }
    }
  }
  button {
    margin: 0 auto;
    margin-top: 48px;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 300px;
    height: 52px;
    gap: 10px;
    padding: 0 10px;
    color: white;
    text-shadow: 2px 2px rgb(116, 116, 116);
    text-transform: uppercase;
    cursor: pointer;
    border: solid 2px #fff;
    letter-spacing: 1px;
    font-weight: 600;
    font-size: 17px;
    background: linear-gradient(
      to right,
      var(--el-color-primary),
      rgb(25, 123, 228)
    );
    border-radius: 50px;
    position: relative;
    overflow: hidden;
    transition: all 0.5s ease;
  }

  button:active {
    transform: scale(0.9);
    transition: all 100ms ease;
  }

  button svg {
    transition: all 0.5s ease;
    z-index: 2;
  }

  .play {
    transition: all 0.5s ease;
    transition-delay: 300ms;
  }

  // button:hover svg {
  //   transform: scale(3) translate(50%);
  // }

  .now {
    position: absolute;
    left: 0;
    transform: translateX(-100%);
    transition: all 0.5s ease;
    z-index: 2;
  }

  button:hover .now {
    transform: translateX(130px);
    transition-delay: 300ms;
  }

  button:hover .play {
    transform: translateX(400%);
    transition-delay: 300ms;
  }
}
</style>
